package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysDeptAddDTO;
import com.framework.system.entity.dto.SysDeptQueryDTO;
import com.framework.system.entity.dto.SysDeptUpdateDTO;
import com.framework.system.entity.vo.SysDeptListVO;
import com.framework.system.entity.vo.SysDeptVO;
import com.framework.system.service.ISysDeptService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysDept")
@AllArgsConstructor
public class SysDeptController {

    private ISysDeptService sysDeptService;

    /**
     * 分页查询部门列表
     */
    @GetMapping("/selectSysDeptWithPage")
    @SaCheckPermission("system:sysDept:selectSysDeptWithPage")
    @AutoLog(module = "部门管理", title = "分页查询部门列表")
    public R<Page<SysDeptListVO>> selectSysDeptWithPage(Page reqPage, SysDeptQueryDTO req) {
        return R.success(sysDeptService.selectSysDeptWithPage(reqPage, req));
    }

    /**
     * 新增部门
     */
    @PostMapping("/addSysDept")
    @SaCheckPermission("system:sysDept:addSysDept")
    @AutoLog(module = "部门管理", title = "新增部门")
    public R<Void> addSysDept(@Validated @RequestBody SysDeptAddDTO req) {
        sysDeptService.addSysDept(req);
        return R.success();
    }

    /**
     * 修改部门
     */
    @PostMapping("/updateSysDept")
    @SaCheckPermission("system:sysDept:updateSysDept")
    @AutoLog(module = "部门管理", title = "修改部门")
    public R<Void> updateSysDept(@Validated @RequestBody SysDeptUpdateDTO req) {
        sysDeptService.updateSysDept(req);
        return R.success();
    }

    /**
     * 删除部门
     */
    @PostMapping("/removeSysDept")
    @SaCheckPermission("system:sysDept:removeSysDept")
    @AutoLog(module = "部门管理", title = "删除部门")
    public R<Void> removeSysDept(@RequestParam String ids) {
        sysDeptService.removeSysDept(ids);
        return R.success();
    }

    /**
     * 查看部门
     */
    @GetMapping("/getSysDept")
    @SaCheckPermission("system:sysDept:getSysDept")
    @AutoLog(module = "部门管理", title = "查看部门")
    public R<SysDeptVO> getSysDept(@RequestParam String id) {
        return R.success(sysDeptService.getSysDept(id));
    }

    /**
     * 查询部门树
     */
    @GetMapping("/selectSysDeptWithTree")
    @AutoLog(module = "部门管理", title = "查询部门树")
    public R<List<Tree<String>>> selectSysDeptWithTree() {
        return R.success(sysDeptService.selectSysDeptWithTree());
    }

    /**
     * 查询部门下拉树列表排除当前节点以及子节点
     */
    @GetMapping("/selectSysDeptAndExcludeNodeWithOptionTree")
    @AutoLog(module = "部门管理", title = "查询部门下拉树列表排除当前节点以及子节点")
    public R<List<Tree<String>>> selectSysDeptAndExcludeNodeWithOptionTree(@RequestParam(required = false) String nodeId) {
        return R.success(sysDeptService.selectSysDeptAndExcludeNodeWithOptionTree(nodeId));
    }

    /**
     * 查询部门下拉树
     */
    @GetMapping("/selectSysDeptWithOptionTree")
    @AutoLog(module = "部门管理", title = "查询部门下拉树")
    public R<List<Tree<String>>> selectSysDeptWithOptionTree() {
        return R.success(sysDeptService.selectSysDeptWithOptionTree());
    }

}

